class Incident < ActiveRecord::Base
	belongs_to :machine
	belongs_to :type_incident

	validates_presence_of :machine_id, :type_incident_id, :date_detection
	validates_associated :machine, :type_incident
	validates_length_of :info_complementaire, :within=>10..100
	
	def validate_numeric_machine
		reg = /[0-9]/ 
		return (reg.match(machine_id.to_s))? true : false
	end
	
	def validate_numeric_type_incident
		reg = /[0-9]/ 
		return (reg.match(type_incident_id.to_s))? true : false
	end
	
	def delais
		if(date_resolution)
			return date_resolution - date_detection
		else
			return 0
		end
	end
	
	def self.total(type)
		inc = Incident.all(:conditions =>{:type_incident_id => type})
		count = 0
		inc.each do |i|
			count = count + i.delais
		end
		return count
	end
end

